package com.tan.androidtest.traceview;

import android.os.Debug;
import android.util.Log;
import android.widget.TextView;

import com.tan.androidtest.R;
import com.tan.androidtest.base.BaseActivity;

/**
 * 测试Android性能分析TraceView
 */

public class TestTraceViewActivity extends BaseActivity {

    int count = 0;
    long longCount = -1;

    @Override
    public int initLayoutId() {
        return R.layout.activity_test_trace_view_layout;
    }

    @Override
    public void initTitle(TextView tv_bar_left, TextView tv_bar_title, TextView tv_bar_right) {
        tv_bar_title.setText("测试Android性能分析TraceView");
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        Debug.startMethodTracing("hello");

        //线程1
        new Thread(new Runnable() {
            @Override
            public void run() {
                printNum();
            }
        }, "printNum_thread").start();
        //线程2
        new Thread(new Runnable() {
            @Override
            public void run() {
                calculate();
            }
        }, "calculate_thread").start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Debug.stopMethodTracing();
    }

    private void printNum() {
        for (int i = 0; i < 20000; i++) {
            print();
        }
    }

    /**
     * 模拟一个自身占用时间不长，但调用却非常频繁的函数
     */
    private void print() {
        count = count++;
    }

    /**
     * 模拟一个调用次数不多，但每次调用却需要花费很长时间的函数
     */
    private void calculate() {
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 1000; j++) {
                if (longCount > 10) {
                    longCount = -10;
                }
            }
        }
        Log.i("==--","done ------ ");
    }


}
